# !/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
sys.path.append("..")
from dataValida.valida import valida
from modelDb.tourists import touristsModel
from modelDb.tourists_info import touristsInfoModel
from modelDb.tourists_actions import touristsActionsModel
import time


#=============================================================
# dmp etl
#=============================================================
class EtlMain(object):
    def __init__(self):
        """
        初始化
        """
        pass

    def start(self):
        """
        入口
        :return:
        """
        num = 0
        while True:
            try:
                #todo kafka 获取数据
                tourists = touristsModel()
                touristsInfo = touristsInfoModel()
                touristsActions = touristsActionsModel()
                etlData = None
                for ii in range(0, 100):
                    etlData = touristsActions.selectEtl(num=num)
                    if not etlData: num +=1
                    else:break


                if not etlData:
                    time.sleep(60)
                    continue

                for data in etlData:
                    touristsData = tourists.findById(data.get("user_id"))
                    # etl_status = 2,3, 数据直接流入画像
                    if touristsData.get("etl_status") in [2, 3]:
                        touristsActions.updateEtl( data=data, data_status=2 )

                    # 人工验证(小区不存在，需要人工验证)
                    elif touristsData.get("etl_status") == 4:
                        touristsInfoData = touristsInfo.findByPhone(touristsData.get("phone"))
                        #商圈和小区都不存在需要人工处理
                        if not touristsInfoData.get("district") and touristsInfoData.get('business_area_ids') == "0":
                            touristsActions.updateEtl(data=data, data_status=4)
                        else:
                            tourists.updateCheckToEtl(id=touristsData.get("id"), etl_status=2)
                            touristsActions.updateCheckToEtl( tourists_id=touristsData.get('id'), data_status=2, phone=touristsData.get("phone"))

                    # 无效数据
                    elif touristsData.get("etl_status") == 5:
                        touristsActions.updateEtl(data=data, data_status=5)

                    # 新增数据， 数据验证（呼叫为空号， 经纪人过滤）
                    elif touristsData.get("etl_status") == 1:
                        validaResult = valida(touristsData).main()
                        #无效 status：呼叫状态，tourist_class：经纪人任
                        if validaResult.get("tourist_class") == 2 or validaResult.get("status") == 4:
                            touristsActions.updateEtl(data=data, data_status=5)
                            validaResult.setdefault('etl_status', 5)
                        else:
                            touristsInfoData = touristsInfo.findByPhone(touristsData.get("phone"))
                            # 商圈和小区都不存在需要人工处理
                            if not touristsInfoData.get("district") and  touristsInfoData.get('business_area_ids')=="0":
                                touristsActions.updateEtl(data=data, data_status=4)
                                validaResult.setdefault('etl_status', 4)
                            else:
                                touristsActions.updateEtl(data=data, data_status=2)
                                validaResult.setdefault('etl_status', 2)

                        tourists.updateEtlValidaById(id=touristsData.get('id'), data=validaResult)
                    print data


            except Exception, e:
                print e

if __name__ == "__main__":
    EtlMain().start()

